﻿using Halcon二次开发.Controller.九点标定功能;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;

namespace Halcon二次开发.View.九点标定
{
    public partial class NinePointCalibrationForm : Form
    {
        public  NinePointCalibrationController controller;
        public NinePointCalibrationForm()
        {
            InitializeComponent();
            controller = new NinePointCalibrationController(this);
        }

        /// <summary>
        /// 新增按钮 末尾新增一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            controller.AddPoint();
            label1.Text = "未矫正";
            label1.ForeColor = Color.Black;
        }
        /// <summary>
        /// 删除按钮 末尾删除一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            controller.DeletePoint();
            label1.Text = "未矫正";
            label1.ForeColor = Color.Black;
        }
        /// <summary>
        /// 运行按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (label1.Text == "已矫正")
                {
                    controller.Run();
                }
                else
                {
                    throw new Exception("算子未矫正!");
                }
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 计算矫正按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (controller.Correct())
                {
                    label1.Text = "已矫正";
                    label1.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            try
            {
                if (controller.SaveMatrix())
                {
                    MessageBox.Show("保存成功!");
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 打开按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            try
            {
                if (controller.OpenMatrix())
                {
                    label1.Text = "已矫正";
                    label1.ForeColor = Color.Red;
                    dataGridView1.Rows.Clear();
                    controller.pointNum = 0;
                }
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        private void NinePointCalibrationForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
    }
}
